<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: swc
 * @Date: 2024-01-30 21:55:02
 * @LastEditors: swc
 * @LastEditTime: 2024-07-30 15:24:09
-->
<template>
  <span :class="{ 'read-only': renderOpts.props.disabled }"
    >{{ $srv.getI18nBySrv(text) }}
  </span>
</template>

<script lang="ts">
import { Tsrv, T_srv, ifs, ts, vue } from '@/services';
import { N_input } from '@/types';

@T_srv.comp
export default class oSelectCell extends vue {
  @T_srv.prop() renderOpts: N_input.I_renderOpts;

  @T_srv.prop() params: any;

  /* 组件名 */
  private name: string = 'oSelectCell';

  public text: string = '';

  private currFieldName: string = '';

  private list: any = [];

  private optionId = null;

  @T_srv.watch('params')
  onParamsChange() {
    this.setValue();
  }

  created() {
    // this.currFieldName = this.params.$table.getTableColumn().fullColumn[
    //   this.params.columnIndex
    // ].title;
    this.currFieldName = this.params.column.property;
    if (!Tsrv.utils.noValue(this.renderOpts.props['optionId'])) {
      // this.getList();
    }
    this.setValue();
  }

  setValue() {
    const rowData = this.params.row;
    const fieldSourceVal = rowData[this.currFieldName];
    const regEx = new RegExp(`^slc\\|${this.currFieldName}`);
    for (const k of Object.keys(rowData)) {
      if (regEx.test(k)) {
        this.text = rowData[k];
      }
    }
    const list =
      this.list.length > 0 ? this.list : this.renderOpts?.props['optionLoaded'];
    if (Tsrv.utils.noValue(this.text)) {
      // this.text = fieldSourceVal;
      if (Tsrv.utils.isNumber(fieldSourceVal) && !Tsrv.utils.noValue(list)) {
        this.text = Tsrv.utils.find(
          list,
          item => item.value === fieldSourceVal
        )?.label;
      } else {
        // this.text = fieldSourceVal;
      }
    } else if (Tsrv.utils.isNumber(this.text)) {
      if (!Tsrv.utils.noValue(list)) {
        this.text = Tsrv.utils.find(
          list,
          item => item.value === this.text
        )?.label;
      }
    }
  }

  private getList(filter: any = null) {
    if (Tsrv.utils.noValue(filter)) {
      filter = null;
    }
    const id = this.renderOpts.props['optionId'];
    const lang = this.$i18n.locale || 'en-US';
    const dbType = this.renderOpts.props['dbType'];
    this.optionId = id;
    Tsrv.getSLCData(id, dbType, lang, filter).then(r => {
      const [b, res, msg] = r;
      !b && msg && this.$msg(msg);
      this.list = res;
      // this.renderOpts.props.optionLoaded = res;
      this.setValue();
    });
  }
}
</script>
<style lang="scss" scoped></style>
